<template>
  <div class="receiving-address">
    <div class="title">我的收货地址</div>
    <div class="default-address-container">
      <div>
        <div class="address-item">收货人：{{ addressData.consignee }}</div>
        <div class="address-item">
          收货地址：{{ addressData.addressDetail }}
        </div>
        <div class="address-item">收货手机号：{{ addressData.phone }}</div>
      </div>

      <el-button type="success" @click="handleChange">更换地址</el-button>
    </div>
    <el-dialog
      title="更换地址"
      v-model="dialogVisible"
      width="580px"
      :before-close="handleClose"
    >
      <el-table
        :data="allAddressData"
        tooltip-effect="dark"
        style="width: 100%; margin-bottom: 14px"
        @selection-change="handleSelectionChange"
        ref="listTable"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column
          label="收货人"
          prop="consignee"
          width="120"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="addressDetail"
          label="收获地址"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="phone"
          label="收获手机号"
          width="120"
          show-overflow-tooltip
        ></el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="success" @click="submitChange">确 定</el-button>
        <el-button @click="handleDefaultAddress">设为默认地址</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import { ElMessage } from "element-plus";
import {
  defaultAddressInfoUpdate,
  selectDefaultByOwnName,
  selectAllAddressByUsername,
} from "../api/address";

// 使用 reactive 创建响应式数据
const addressData = reactive({
  consignee: "",
  addressDetail: "",
  phone: "",
  isDefault: false,
  addressId: "",
});
const allAddressData = ref([]);
const dialogVisible = ref(false);
const listTable = ref(null);
const selectedAddress = ref(null); // 用于存储当前选中的地址

// 获取所有地址
const getAllAddress = async () => {
  try {
    const res = await selectAllAddressByUsername();
    allAddressData.value = res.data;
  } catch (err) {
    ElMessage.error("获取地址失败");
  }
};

// 获取默认地址
const getDefaultAddress = async () => {
  try {
    const res = await selectDefaultByOwnName({});
    addressData.consignee = res.data.consignee;
    addressData.addressDetail = res.data.addressDetail;
    addressData.phone = res.data.phone;
    addressData.addressId = res.data.addressId; // 确保获取地址ID
  } catch (err) {
    ElMessage.error("获取默认地址失败");
  }
};

// 处理更换地址
const handleChange = () => {
  dialogVisible.value = true;
};

// 提交地址更改
const submitChange = () => {
  if (selectedAddress.value) {
    addressData.consignee = selectedAddress.value.consignee;
    addressData.addressDetail = selectedAddress.value.addressDetail;
    addressData.phone = selectedAddress.value.phone;
    addressData.id = selectedAddress.value.id; // 更新地址ID

    dialogVisible.value = false;
  }
};

// 设为默认地址
const handleDefaultAddress = () => {
  if (selectedAddress.value && selectedAddress.value.id) {
    defaultAddressInfoUpdate({
      id: selectedAddress.value.id,
      consignee: selectedAddress.value.consignee,
      addressDetail: selectedAddress.value.addressDetail,
      phone: selectedAddress.value.phone,
    })
      .then(() => {
        ElMessage.success("修改成功");
        getDefaultAddress();
      })
      .catch(() => {
        ElMessage.error("修改失败");
      });
  } else {
    ElMessage.warning("请选择一个地址");
  }
};

// 关闭对话框
const handleClose = () => {
  dialogVisible.value = false;
};

// 处理选择变化
const handleSelectionChange = (selection) => {
  if (selection.length > 1) {
    let delRow = selection.shift();
    listTable.value.toggleRowSelection(delRow, false); // 设置这一行取消选中
  }
  if (selection.length > 0) {
    selectedAddress.value = selection[0];
  } else {
    selectedAddress.value = null;
  }
};

// 生命周期钩子
onMounted(() => {
  getAllAddress();
  getDefaultAddress();
});
</script>

<style lang="less" scoped>
.receiving-address {
  position: relative;
  .title {
    border-bottom: 2px solid #f2f2f2;
    padding: 10px 0px;
    font-size: 18px;
  }
}
.default-address-container {
  border: 1px dashed darkseagreen;
  border-radius: 6px;
  padding: 10px 20px;
  display: flex;
  margin-top: 20px;
  align-items: center;
  justify-content: space-between;
  .address-item {
    line-height: 30px;
    height: 30px;
    margin-right: 50px;
  }
}
</style>